package com.scalar.forecast.salesForecastEntry.actionCommands;

import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.salesForecastEntry.forms.SalesForecastEntryForm;
import com.scalar.forecast.salesForecastEntry.dao.SalesForecastEntryDAO;
import com.scalar.forecast.salesForecastEntry.*;
import com.scalar.chiptrack.utils.ConfigManager;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.text.DecimalFormat;

/**
 * User: Sujan
 * Date: Sept 29, 2005
 * Time: 5:00:42 PM
 */
public class SalesForecastEntryCalcRunningTotalsCommand extends ActionCommand
{
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception
    {
        SalesForecastEntryForm forecastEntryForm    = ( SalesForecastEntryForm ) form;

        int startMonth  = forecastEntryForm.getMonth();
        int startYear   = forecastEntryForm.getYear();
        int showNext    = forecastEntryForm.getShowNext();

        ArrayList slotNames = SalesForecastEntryDAO.getSlotNames ( startMonth, startYear, showNext );
        request.setAttribute ( "slotNames", slotNames );

        AutoGrowingLinkedList rowDataList = forecastEntryForm.getRowDataList();
        int rowDataListSize = ( rowDataList == null ) ? 0 : rowDataList.size();
        RowData rowData = null;
        SlotData slotData   = null;
        AutoGrowingLinkedList slotDataList = null;
        AutoGrowingLinkedList rowDataRunningTotal = new AutoGrowingLinkedList( NewLabelValueBean.class );
        double runningTotal = 0.0d;
        DecimalFormat decimalFormat;
		if(ConfigManager.getBooleanProperty(ConfigManager.FORECAST_SHOW_PRICE)) {
			decimalFormat = new DecimalFormat( "###########0.00" );
		} else {
			decimalFormat = new DecimalFormat( "###########0" ); 
		}
		for ( int slot = 0; slot < showNext; slot++ )
        {
            runningTotal = 0.0d;
            for ( int row = 0; row < rowDataListSize; row++ )
            {
                rowData = (RowData)rowDataList.get ( row );
                if ( rowData != null )
                {
                    slotDataList = rowData.getSlotDataList();
                    if ( slotDataList != null && slotDataList.size() > slot )
                    {
                        slotData = (SlotData)slotDataList.get ( slot );
                        if ( slotData != null )
                        {
							if(ConfigManager.getBooleanProperty(ConfigManager.FORECAST_SHOW_PRICE)) {
                            	runningTotal = runningTotal + ( slotData.getQty() * slotData.getPrice() );
							} else {
								runningTotal = runningTotal + slotData.getQty();
							}
                        }
                    }
                }
            }
            rowDataRunningTotal.add ( new NewLabelValueBean ( decimalFormat.format( runningTotal ), decimalFormat.format( runningTotal ) ) );
        }
        forecastEntryForm.setRowDataListRunningTotal( rowDataRunningTotal );

        ForecastData    forecastData            = null;
        AutoGrowingLinkedList forecastDataRunningTotal = null;
        AutoGrowingLinkedList forecastDataList  = forecastEntryForm.getForecastDataList();
        int forecastDataListSize   = ( forecastDataList == null ) ? 0 : forecastDataList.size();
        for ( int i = 0; i < forecastDataListSize; i++ )
        {
            forecastData    = (ForecastData)forecastDataList.get ( i );
            forecastDataRunningTotal = new AutoGrowingLinkedList( NewLabelValueBean.class );
            rowDataList = forecastData.getRowDataList();
            rowDataListSize = ( rowDataList == null ) ? 0 : rowDataList.size();
            for ( int slot = 0; slot < showNext; slot++ )
            {
                runningTotal = 0.0d;
                for ( int row = 0; row < rowDataListSize; row++ )
                {
                    rowData = (RowData)rowDataList.get ( row );
                    if ( rowData != null )
                    {
                        slotDataList = rowData.getSlotDataList();
                        if ( slotDataList != null && slotDataList.size() > slot )
                        {
                            slotData = (SlotData)slotDataList.get ( slot );
                            if ( slotData != null )
                            {
								if(ConfigManager.getBooleanProperty(ConfigManager.FORECAST_SHOW_PRICE)) {
                                	runningTotal = runningTotal + ( slotData.getQty() * slotData.getPrice() );
								} else {
									runningTotal = runningTotal + slotData.getQty();
								}
                            }
                        }
                    }
                }
                forecastDataRunningTotal.add ( new NewLabelValueBean ( decimalFormat.format( runningTotal ), decimalFormat.format( runningTotal ) ) );
            }
            forecastData.setRunningTotal( forecastDataRunningTotal );
        }
        return mapping.findForward( "salesForecastEntry" );
    }
}
